package com.such.kit.database.bean;

import java.io.Serializable;
import com.such.kit.string.StringCommons;
import com.such.kit.validate.ValidateCommons;

/**
 * @author SUCH
 * <pre>
 * jdbc 查询参数
 * jdbc 查询参数的最终目标是构建一个 where 条件片段
 * 多个 jdbc 查询参数间跟据 {@link #groupName} 决定使用 and 或者 or 连接
 * 条件片段的构建一边有两种方式：值绑定、值接拼接字符串
 * 应对这两种方式引入了 {@link bindMode}、{@link addQuotes}，优先级 {@link bindMode} > {@link addQuotes}
 * </pre>
 */
public class JdbcQueryParam implements Serializable {

	/** GROUP_NAME_DEFAULT：默认的组名 */
	public static final String GROUP_NAME_DEFAULT = "default";
	/** BIND_MODE_NORMATIVE：jdbc 标准的值绑定（使用 ? 占位符） */
	public static final String BIND_MODE_NORMATIVE = "normative";
	/** BIND_MODE_SPLICE：将值作为字符串拼接，默认在值前后添加单引号（'） */
	public static final String BIND_MODE_STRING = "string";
	/** BIND_MODE_DEFAULT：默认（不做处理）的字符串拼接 */
	public static final String BIND_MODE_DEFAULT = "default";

	private static final long serialVersionUID = 4680184581891657554L;

	/** field 字段名 */
	private String field;
	/** operator 运算符，默认为 = */
	private String operator = "=";
	/** value 值 */
	private String value;
	/**
	 * bindMode：值绑定模式 [默认为 {@link #BIND_MODE_NORMATIVE}]
	 * <pre>
	 * 生成 where 条件片段的值绑定模式：
	 * {@link #BIND_MODE_NORMATIVE}
	 * {@link #BIND_MODE_STRING}
	 * {@link #BIND_MODE_DEFAULT}
	 * </pre>
	 */
	private String bindMode = BIND_MODE_NORMATIVE;
	/** 
	 * groupName 条件组 [默认为 {@link #GROUP_NAME_DEFAULT}]
	 * <pre>
	 * 同组条件间使用 and 连接
	 * 不同组条件间使用 or 连接
	 * </pre>
	 */
	private String groupName = GROUP_NAME_DEFAULT;

	/**
	 * @param field [参考 {@link #field}]
	 * @param value [参考 {@link #value}]
	 */
	public JdbcQueryParam(String field, String value) {
		this.field = field;
		this.value = value;
	}

	/**
	 * @param field [参考 {@link #field}]
	 * @param operator [参考 {@link #operator}]
	 * @param value [参考 {@link #value}]
	 */
	public JdbcQueryParam(String field, String operator, String value) {
		this.field = field;
		this.operator = operator;
		this.value = value;
	}

	/**
	 * @param field [参考 {@link #field}]
	 * @param operator [参考 {@link #operator}]
	 * @param value [参考 {@link #value}]
	 * @param groupName [参考 {@link #groupName}]
	 */
	public JdbcQueryParam(String field, String operator, String value, String groupName) {
		this.field = field;
		this.operator = operator;
		this.value = value;
		this.groupName = groupName;
	}

	/**
	 * @param field [参考 {@link #field}]
	 * @param operator [参考 {@link #operator}]
	 * @param value [参考 {@link #value}]
	 * @param bindMode [参考 {@link #bindMode}]
	 * @param groupName [参考 {@link #groupName}]
	 */
	public JdbcQueryParam(String field, String operator, String value, String bindMode, String groupName) {
		this.field = field;
		this.operator = operator;
		this.value = value;
		this.bindMode = bindMode;
		this.groupName = groupName;
	}

	/** [参考 {@link #field}] */
	public String getField() {
		return this.field;
	}

	/** [参考 {@link #field}] */
	public void setField(String field) {
		this.field = field;
	}

	/** [参考 {@link #operator}] */
	public String getOperator() {
		return this.operator;
	}

	/** [参考 {@link #operator}] */
	public void setOperator(String operator) {
		this.operator = operator;
	}

	/** [参考 {@link #value}] */
	public String getValue() {
		return this.value;
	}

	/** [参考 {@link #value}] */
	public void setValue(String value) {
		this.value = value;
	}

	/** [参考 {@link #bindMode}] */
	public String getBindMode() {
		return this.bindMode;
	}

	/** [参考 {@link #bindMode}] */
	public void setBindMode(String bindMode) {
		this.bindMode = bindMode;
	}

	/** [参考 {@link #groupName}] */
	public String getGroupName() {
		return this.groupName;
	}

	/** [参考 {@link #groupName}] */
	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	/**
	 * <pre>
	 * 构建条件判断
	 * </pre>
	 * @return 条件片段
	 */
	public String getFragment() {
		if (ValidateCommons.isEmpty(this.value, false)) {
			this.value = "";
		}
		String operator = " ";
		if (ValidateCommons.isNotEmpty(this.operator)) {
			operator = StringCommons.merger(" ", this.operator, " ");
		}
		if (BIND_MODE_NORMATIVE.equals(this.bindMode)) {
			return StringCommons.merger(this.field, operator, "?");
		} else if (BIND_MODE_STRING.equals(this.bindMode)) {
			return StringCommons.merger(this.field, operator, "'", this.value, "'");
		} else {
			return StringCommons.merger(this.field, this.operator, this.value);
		}
	}

}
